/* CsJni - .NET / Java Bridge
 * 
 * Copyright 2010 Robert E. Burke Licensed under the Apache License, Version 
 * 2.0 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable 
 * law or agreed to in writing, software distributed under the License is 
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 * KIND, either express or implied. See the License for the specific language 
 * governing permissions and limitations under the License. 
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace CsJni
{
    public sealed class JniDoubleArray : JniArray
    {
        internal JniDoubleArray(JniEnvironment environment, IntPtr handle, JniObjectLifecycle lifecycle)
            : base(environment, handle, lifecycle)
        {
        }

        public override JniConcreteObjectType ConcreteType
        {
            get { return JniConcreteObjectType.DoubleArray; }
        }

        public static JniDoubleArray NewDoubleArray(
            JniEnvironment environment,
            int length
            )
        {
            VerifyEnvironment(environment);
            IntPtr result = JniEnvironment.Functions.NewDoubleArray(environment.Handle, length);
            if (IntPtr.Zero == result)
            {
                JniThrowable.ThrowCurrentException(environment); // null result means an exception occurred
            }
            // EXCEPTION CHECK: Do nothing, already checked for exception above.
            return new JniDoubleArray(environment, result, JniObjectLifecycle.Local);
        }

        public double[] ExtractDoubleArray(JniEnvironment environment)
        {
            return ExtractPrimitiveArray<double>(environment);
        }

        public static JniDoubleArray PackDoubleArray(JniEnvironment environment, double[] sourceArray)
        {
            VerifyEnvironment(environment);
            if (sourceArray == null) { return null; }
            JniDoubleArray arrayHandle = NewDoubleArray(environment, sourceArray.Length);
            if (sourceArray.Length > 0)
            {
                arrayHandle.PackPrimitiveArray<double>(environment, sourceArray);
            }
            return arrayHandle;
        }

        public double[] GetDoubleArrayRegion(
            JniEnvironment environment,
            int start,
            int length
            )
        {
            VerifyEnvironment(environment);
            double[] result = new double[length];
            JniEnvironment.Functions.GetDoubleArrayRegion(environment.Handle, this.Handle, start, length, result);
            JniThrowable.CheckException(environment);
            return result;
        }

        public void SetDoubleArrayRegion(
            JniEnvironment environment,
            int start,
            double[] buffer
            )
        {
            VerifyEnvironment(environment);
            VerifyInputArray(buffer);
            int length = buffer.Length;
            JniEnvironment.Functions.SetDoubleArrayRegion(environment.Handle, this.Handle, start, length, buffer);
            JniThrowable.CheckException(environment);
        }
    }
}
